<template>
  <view class="dashboard-container">
    <!-- 欢迎区域 -->
    <view class="welcome-section">
      <view class="welcome-content">
        <text class="welcome-title">欢迎回来</text>
        <text class="welcome-subtitle">{{ displayName }}</text>
      </view>
      <view class="user-avatar">
        <image :src="userAvatar" class="avatar-img" mode="aspectFill" />
      </view>
    </view>

    <!-- 统计卡片 -->
    <view class="stats-section">
      <view class="stats-grid">
        <view v-for="stat in statsData" :key="stat.id" class="stat-card">
          <view class="stat-icon" :style="{ backgroundColor: stat.color }">
            <text class="stat-icon-text">{{ stat.icon }}</text>
          </view>
          <view class="stat-content">
            <text class="stat-value">{{ stat.value }}</text>
            <text class="stat-label">{{ stat.label }}</text>
          </view>
        </view>
      </view>
    </view>

    <!-- 快速操作 -->
    <view class="quick-actions">
      <text class="section-title">快速操作</text>
      <view class="actions-grid">
        <view
          v-for="action in quickActions"
          :key="action.id"
          class="action-card"
          @click="handleQuickAction(action)"
        >
          <view class="action-icon">
            <text class="action-icon-text">{{ action.icon }}</text>
          </view>
          <text class="action-label">{{ action.label }}</text>
        </view>
      </view>
    </view>

    <!-- 最近活动 -->
    <view class="recent-activities">
      <view class="section-header">
        <text class="section-title">最近活动</text>
        <text class="view-all" @click="viewAllActivities">查看全部</text>
      </view>
      <view class="activities-list">
        <view v-for="activity in recentActivities" :key="activity.id" class="activity-item">
          <view class="activity-dot" :style="{ backgroundColor: activity.color }"></view>
          <view class="activity-content">
            <text class="activity-title">{{ activity.title }}</text>
            <text class="activity-time">{{ activity.time }}</text>
          </view>
        </view>
      </view>
    </view>

    <!-- 底部操作栏 -->
    <view class="bottom-actions">
      <button class="logout-btn" @click="handleLogout">
        <text>退出登录</text>
      </button>
    </view>
  </view>
</template>

<script setup lang="ts">
import { ref, computed, onMounted } from 'vue'
import { authGetters, authActions } from '@/store/modules/auth'
import { navigation } from '@/utils/navigation'

// 响应式数据
const statsData = ref([
  {
    id: 1,
    icon: '👥',
    label: '总用户数',
    value: '1,234',
    color: '#007aff'
  },
  {
    id: 2,
    icon: '🏫',
    label: '学校数量',
    value: '15',
    color: '#34c759'
  },
  {
    id: 3,
    icon: '📚',
    label: '班级数量',
    value: '89',
    color: '#ff9500'
  },
  {
    id: 4,
    icon: '👨‍🎓',
    label: '学生数量',
    value: '2,456',
    color: '#ff3b30'
  }
])

const quickActions = ref([
  {
    id: 1,
    icon: '👤',
    label: '用户管理',
    action: 'user-manage'
  },
  {
    id: 2,
    icon: '🏫',
    label: '班级管理',
    action: 'class-manage'
  },
  {
    id: 3,
    icon: '📊',
    label: '数据统计',
    action: 'statistics'
  },
  {
    id: 4,
    icon: '⚙️',
    label: '系统设置',
    action: 'settings'
  },
  {
    id: 5,
    icon: '📋',
    label: '操作日志',
    action: 'logs'
  },
  {
    id: 6,
    icon: '📥',
    label: '数据导入',
    action: 'import'
  }
])

const recentActivities = ref([
  {
    id: 1,
    title: '新增用户：张老师',
    time: '2分钟前',
    color: '#34c759'
  },
  {
    id: 2,
    title: '更新班级：三年级1班',
    time: '15分钟前',
    color: '#007aff'
  },
  {
    id: 3,
    title: '导入学生数据：50条',
    time: '1小时前',
    color: '#ff9500'
  },
  {
    id: 4,
    title: '系统维护完成',
    time: '2小时前',
    color: '#8e8e93'
  }
])

// 计算属性
const displayName = computed(() => authGetters.displayName.value)
const userAvatar = computed(() => authGetters.avatar.value)

// 生命周期
onMounted(() => {
  // 加载数据
  loadDashboardData()
})

// 方法
/**
 * 加载仪表板数据
 */
const loadDashboardData = async () => {
  try {
    // TODO: 调用API获取真实数据
    console.log('加载仪表板数据...')
  } catch (error) {
    console.error('加载仪表板数据失败:', error)
  }
}

/**
 * 处理快速操作
 */
const handleQuickAction = (action: any) => {
  switch (action.action) {
    case 'user-manage':
      navigation.toUserManage()
      break
    case 'class-manage':
      navigation.toClassManage()
      break
    case 'statistics':
      // TODO: 导航到统计页面
      break
    case 'settings':
      navigation.toSystemSettings()
      break
    case 'logs':
      navigation.toLogs()
      break
    case 'import':
      // TODO: 导航到导入页面
      break
    default:
      console.log('未知操作:', action.action)
  }
}

/**
 * 查看所有活动
 */
const viewAllActivities = () => {
  navigation.toLogs()
}

/**
 * 处理退出登录
 */
const handleLogout = async () => {
  uni.showModal({
    title: '确认退出',
    content: '确定要退出登录吗？',
    success: (res) => {
      if (res.confirm) {
        authActions.logout()
      }
    }
  })
}
</script>

<style lang="scss" scoped>
.dashboard-container {
  min-height: 100vh;
  background: #f7f8fa;
  padding: 20px;
  padding-bottom: 100px;
}

// 欢迎区域
.welcome-section {
  display: flex;
  justify-content: space-between;
  align-items: center;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  border-radius: 16px;
  padding: 24px;
  margin-bottom: 24px;
  box-shadow: 0 4px 20px rgba(102, 126, 234, 0.3);

  .welcome-content {
    .welcome-title {
      display: block;
      color: white;
      font-size: 24px;
      font-weight: 600;
      margin-bottom: 4px;
    }

    .welcome-subtitle {
      display: block;
      color: rgba(255, 255, 255, 0.8);
      font-size: 16px;
    }
  }

  .user-avatar {
    .avatar-img {
      width: 60px;
      height: 60px;
      border-radius: 30px;
      border: 3px solid rgba(255, 255, 255, 0.3);
    }
  }
}

// 统计卡片
.stats-section {
  margin-bottom: 24px;

  .stats-grid {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 16px;

    .stat-card {
      display: flex;
      align-items: center;
      background: white;
      border-radius: 12px;
      padding: 20px;
      box-shadow: 0 2px 12px rgba(0, 0, 0, 0.06);

      .stat-icon {
        width: 48px;
        height: 48px;
        border-radius: 12px;
        display: flex;
        align-items: center;
        justify-content: center;
        margin-right: 16px;

        .stat-icon-text {
          font-size: 24px;
        }
      }

      .stat-content {
        flex: 1;

        .stat-value {
          display: block;
          font-size: 20px;
          font-weight: 600;
          color: #333;
          margin-bottom: 2px;
        }

        .stat-label {
          display: block;
          font-size: 14px;
          color: #666;
        }
      }
    }
  }
}

// 快速操作
.quick-actions {
  margin-bottom: 24px;

  .section-title {
    display: block;
    font-size: 18px;
    font-weight: 600;
    color: #333;
    margin-bottom: 16px;
  }

  .actions-grid {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 16px;

    .action-card {
      display: flex;
      flex-direction: column;
      align-items: center;
      background: white;
      border-radius: 12px;
      padding: 20px 16px;
      box-shadow: 0 2px 12px rgba(0, 0, 0, 0.06);
      transition: all 0.3s ease;
      cursor: pointer;

      &:active {
        transform: translateY(2px);
        box-shadow: 0 1px 6px rgba(0, 0, 0, 0.1);
      }

      .action-icon {
        width: 48px;
        height: 48px;
        background: #f0f9ff;
        border-radius: 12px;
        display: flex;
        align-items: center;
        justify-content: center;
        margin-bottom: 12px;

        .action-icon-text {
          font-size: 24px;
        }
      }

      .action-label {
        font-size: 14px;
        color: #333;
        text-align: center;
      }
    }
  }
}

// 最近活动
.recent-activities {
  .section-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 16px;

    .section-title {
      font-size: 18px;
      font-weight: 600;
      color: #333;
    }

    .view-all {
      font-size: 14px;
      color: #007aff;
      cursor: pointer;
    }
  }

  .activities-list {
    background: white;
    border-radius: 12px;
    padding: 20px;
    box-shadow: 0 2px 12px rgba(0, 0, 0, 0.06);

    .activity-item {
      display: flex;
      align-items: center;
      padding: 12px 0;
      border-bottom: 1px solid #f0f0f0;

      &:last-child {
        border-bottom: none;
      }

      .activity-dot {
        width: 8px;
        height: 8px;
        border-radius: 4px;
        margin-right: 16px;
        flex-shrink: 0;
      }

      .activity-content {
        flex: 1;

        .activity-title {
          display: block;
          font-size: 15px;
          color: #333;
          margin-bottom: 2px;
        }

        .activity-time {
          display: block;
          font-size: 13px;
          color: #999;
        }
      }
    }
  }
}

// 底部操作栏
.bottom-actions {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  background: white;
  padding: 20px;
  padding-bottom: calc(20px + env(safe-area-inset-bottom));
  box-shadow: 0 -2px 12px rgba(0, 0, 0, 0.06);

  .logout-btn {
    width: 100%;
    height: 48px;
    background: #ff3b30;
    border: none;
    border-radius: 12px;
    color: white;
    font-size: 16px;
    font-weight: 500;
  }
}

// 响应式设计
@media (max-width: 400px) {
  .dashboard-container {
    padding: 16px;
  }

  .welcome-section {
    flex-direction: column;
    text-align: center;

    .user-avatar {
      margin-top: 16px;
    }
  }

  .actions-grid {
    grid-template-columns: repeat(2, 1fr) !important;
  }
}
</style>